package com.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtil {
	/**
	 * 创建一个获取当期时间的方法，以yyyy-MM-dd HH:mm:ss格式便于存入和取出
	 * @return
	 */
	public String getTime(){
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time =sdf.format(date);
		return time;
	}
	
	/**
	 * 
	 * @param time
	 * @return
	 */
	
	public String showTime(String time){
		String str=null;
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");	
		StringBuffer sb=new StringBuffer();
		try {
			
			long oldTime=sdf.parse(time).getTime();
			long newTime=new Date().getTime();
			long t=(newTime-oldTime)/1000;
			if(t>=86400){
				str=time.substring(0,16);
			}else if(t>=3600 && t<86400){
				long hour=t/3600;
				long min=(t%(3600))/60;
				str=sb.append(hour).append("小时").append(min).append("分钟前").toString();
			}else if(t>=60&& t<3600){
				long min=t/60;
			str=sb.append(Long.toString(min)).append("分钟前").toString();
			}else if(t>10 && t<60 ){
				str=sb.append(t).append("秒前").toString();
			}else{
				str="刚刚";
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return str;
	}
}
